/**
 * @fileOverview
 * webpack.config.js
 * ---------------------------
 * @author iNahoo
 * @since 2017/10/19.
 */
"use strict";

const path = require('path');
const glob = require('glob');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

process.env.BABEL_ENV = 'development';
process.env.NODE_ENV = 'development';

const HTMLS = [];
const INDEX = [];
const entries = (() => {
    const en = {};
    glob.sync('src/**/*.js').forEach(p => {
        const n = p.slice(4, -'/index.js'.length);
        en[n] = path.resolve(__dirname, p);

        HTMLS.push(
            new HtmlWebpackPlugin({
                title: n,
                template: './config/template.html',
                filename: `${n}.html`,
                chunks: [n],
            })
        );

        INDEX.push({
            name: n,
            target: `${n}.html`,
        })
    });

    return en;
})();

module.exports = {
    entry: entries,
    output: {
        filename: '[name].js',
        path: path.resolve(__dirname, 'dist')
    },

    plugins: [
        new CleanWebpackPlugin(['dist']),
        new HtmlWebpackPlugin({
            title: 'F2 Lab',
            template: './config/index.html',
            chunks: [''],
            INDEX,
        }),
        ...HTMLS,
    ],

    devServer: {
        contentBase: './dist'
    },

    devtool: 'inline-source-map',
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: [/node_modules/],
                use: 'babel-loader',
            },
            {
                test: /\.(css)$/,
                exclude: [/node_modules/],
                use: ['style-loader','css-loader'],
            }
        ]
    },

    resolve: {
        extensions: [".js", ".json", '/index.js']
    }
};
